using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace gears
{
    class Menu
    {
        Texture2D startTexture, optionsTexture, exitTexture;

        Vector2 startPosition, optionsPosition, exitPosition;


        Game1.State innerState = Game1.State.Menu;

        Texture2D menu_background;


        public Menu()
        {

            startPosition = new Vector2(Game1.screenHeight / 2 - 100, Game1.screenWidth / 2 - 140);
            optionsPosition = new Vector2(startPosition.X, startPosition.Y + 100);
            exitPosition = new Vector2(startPosition.X, startPosition.Y + 200);
        }


        public Game1.State InnerState
        {
            set { innerState = value; }
        }

        public Texture2D StartTexture
        {
            set { startTexture = value; }
        }
        public Texture2D OptionsTexture
        {
            set { optionsTexture = value; }
        }
        public Texture2D ExitTexture
        {
            set { exitTexture = value; }
        }
        

        public void Update(GameTime gameTime)
        {
            //Transition Effect~~~~~~~~~~~~~~~~~~~~~~~~~~
            if (Game1.transition)
            {
                Game1.alpha -= 0.05f;

                if (Game1.alpha <= Game1.globalAlpha)
                {
                    Game1.alpha = Game1.globalAlpha;
                    Game1.transition = false;
                    this.innerState = Game1.State.Menu;
                    //myOptions.InnerState = State.Options;
                }
            }
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            
            //Transition Effect~~~~~~~~~~~~~~~~~~~~~~~~~~~
            if (innerState != Game1.State.Menu && Game1.transition == false)
            {
                Game1.alpha += 0.05f;

                if (Game1.alpha > 1f)
                {
                    Game1.alpha = 1f;
                    Game1.transition = true;
                    Game1.gameState = innerState;
                }
            }
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            
            //~~~Set the regions for the input~~~~~~~~~~
            Rectangle startRect = new Rectangle((int)startPosition.X, (int)startPosition.Y, startTexture.Width, startTexture.Height);
            Rectangle optionsRect = new Rectangle((int)optionsPosition.X, (int)optionsPosition.Y, optionsTexture.Width, optionsTexture.Height);
            Rectangle exitRect = new Rectangle((int)exitPosition.X, (int)exitPosition.Y, exitTexture.Width, exitTexture.Height);

            

            //Check if a region is selected and act accordingly~~~~~~~~~~~
            TouchCollection touchLocations = TouchPanel.GetState();
            foreach (TouchLocation touchLocation in touchLocations)
            {
                if ((touchLocation.State == TouchLocationState.Pressed))
                {
                    Point touchPoint = new Point((int)touchLocation.Position.X, (int)touchLocation.Position.Y);

                    if (startRect.Contains(touchPoint))
                    {
                        Game1.mySoundEffects.Play("menuSound");
                        innerState = Game1.State.Game;
                    }
                    if (optionsRect.Contains(touchPoint))
                    {
                        Game1.mySoundEffects.Play("menuSound");
                        innerState = Game1.State.Options;
                    }
                    if (exitRect.Contains(touchPoint))
                    {
                        Game1.mySoundEffects.Play("menuSound");
                        innerState = Game1.State.Exit;
                    }

                }
            }

            //Check if Back button is pressed
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Game1.mySoundEffects.Play("menuSound");
                innerState = Game1.State.Exit;
            }  
 
           
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            
            spriteBatch.Draw(startTexture, startPosition, Color.White);
            spriteBatch.Draw(optionsTexture, optionsPosition, Color.White);
            spriteBatch.Draw(exitTexture, exitPosition, Color.White);
            spriteBatch.Draw(Game1.backgroundTexture, new Rectangle(0, 0, Game1.screenHeight, Game1.screenWidth), Color.White * Game1.alpha);
        }
    }
}
